<!--<a href="#/layui/fenhuiguanli" class="layui-btn layui-btn-xs layui-btn-primary" style="margin: 20px 0 0 100px;">
  <i class="layui-icon">&#xe65c;</i>
</a>-->

<form action="" class="layui-form">
	<div class="layui-fluid">
		<div class="layui-card">
			<!--<div class="layui-container">-->
				<div class="layui-row">
			    <div class="layui-col-md12">
			    	<div class="layui-card-header">
			    		<h2>
			      <a href="#/layui/hangyeList" class="layui-btn layui-btn-xs layui-btn-primary" style="margin: -0px 0 0 0;border: none;background-color: rgba(0,0,0,0);">
															  <i class="layui-icon">&#xe65c;</i>
															</a>
															添加行业
						</h2>
			      </div>
			      
			      
			      	<div class="layui-card-body">
			      		<div class="layui-form-item">
			      			<div class="layui-form-label">上级分类：</div>
			      			<div class="layui-input-block">
			      				<div class="select25">
			      					<select name="classItem" class="classItem" lay-verify="" lay-filter="class_item">
											  <option value="">顶级分类</option>
											</select> 
			      				</div>
			      				
			      			</div>
			      		</div>
			      		<div class="layui-form-item">
			      			<div class="layui-form-label">分类名称：<span class="haveTo">*</span></div>
			      			<div class="layui-input-block">
			      				<input type="text" name="className" lay-verify="required" class="layui-input className input33" > 
			      			</div>
			      		</div>
			      		<div class="layui-form-item">
			      			<div class="layui-form-label">排序：</div>
			      			<div class="layui-input-block">
			      				<input type="text" name="sort" class="layui-input sort input33" >
			      			</div>
			      		</div>
			      	</div>
			      	
			      	<table class="layui-table" style="table-layout:fixed" lay-size="lg" lay-skin="nob">
					      <colgroup>
							    <col width="62">
							    <col width="500">
							  </colgroup>
					      <tbody>
					      	
					      	
					      	<!--<tr>
					      		<td class="small_td">上级分类：</td>
					      		<td>
					      			<table class="layui-table classify" lay-skin="nob">
					      				<tbody>
					      					<tr>
					      						<td style="width: 200px;">
					      							<div class="select25">
						      							<select name="classItem" class="classItem" lay-verify="" lay-filter="class_item">
																  <option value="">顶级分类</option>
																</select> 
															</div>
					      						</td>
					      					</tr>
					      				</tbody>
					      			</table>
					      		</td>
					      	</tr>-->
					      	<!--<tr>
					      		<td class="small_td">分类名称：</td>
					      		<td><input type="text" name="className" class="layui-input className input33" ></td>
					      	</tr>
					      	<tr>
					      		<td class="small_td">排序：</td>
					      		<td><input type="text" name="sort" class="layui-input sort input33" ></td>
					      	</tr>-->
					      	
						    
						  </tbody>
					      	
				      </table>
			    </div>
			    
			    <div class="layui-col-md10">
			    	<div class="btn">
			    		<button class="layui-btn confirm" lay-submit lay-filter="queding" type="button" >确定</button>
			    	</div>
							
			    </div>
			    
			  </div>
			</div>

		</div>
	</div>
</form>

<script>
	let className = document.querySelector(".className");
	let sort = document.querySelector(".sort");
	let confirm = document.querySelector(".confirm");
	let classItem = document.querySelector(".classItem");
	let pcode = "00";
	
	let _obj = {
	  "Code": "",
	  "Name": "",
	  "Descrition": "",
	  "Pcode": "",
	  "CreateTime": "",
	  "UpdateTime": "",
	  "OrderId": ""
	}
	
	renderForm();
	layui.use(['jquery', 'mockjs', 'table', 'sidebar', 'form','route'], function () {
		var $ = layui.jquery,
      layer = layui.layer,
      table = layui.table,
      sidebar = layui.sidebar,
      route = layui.route,
      params = route.params(),
      form = layui.form;
      
      form.on('select(class_item)', function(data){
      	if(data.value==""){
      		pcode = "00"
      	}else {
      		pcode=data.value;
      	}
      	
      	console.log("pcode",pcode)
      })
      
      $.ajax({//异步请求返回给后台
    	  url:"https://xnsmb.xnsoft.net.cn/api/GetXn_Trade?FatherId=00",
    	  type:'get',
    	  data:{},
    	  beforeSend: function(request) {
            
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
    	  dataType:'json',
    	  success:function(data){
//  	  	$(".friHang").empty();
//  	  	console.log(friHang)
//  		  let p = document.createElement('option');	
//  		  p.value='';
//  		  p.innerText="一级行业";
//  		  friHang.appendChild(p)
    		  data.forEach((el,index)=>{
    		  	let op = document.createElement('option');
    		  		op.id = el.Id;
					op.value = el.Code;
	    		  	op.innerText = el.Name;
	    		  	classItem.appendChild(op)
    		  })
			$(".classItem").empty();
			let p = document.createElement('option');	
			p.value='00';
			p.innerText="顶级分类";
			classItem.appendChild(p)
			data.forEach((el,index)=>{
			  	let op = document.createElement('option');
					op.value = el.Code;
	    		  	op.innerText = el.Name;
	    		  	classItem.appendChild(op)
			  })
			
			if(params){
				$(".classify").find("option[value='"+data.Pcode+"']").prop("selected",true);
			}
			
    		  renderForm();
    	  }
    	  
      });
      
      if(params){
      	console.log("params.id",params.id)
      
	      $.ajax({//异步请求返回给后台
	    	  url:"https://xnsmb.xnsoft.net.cn/api/Admin_Xn_Trade",
	    	  type:'get',
	    	  beforeSend: function(request) {
            
            request.setRequestHeader("Authorization", 'bearer ' + localStorage.getItem("token"));
        },
	    	  data:{id:params.id},
	    	  dataType:'json',
	    	  success:function(data){
	    	  	console.log("data",data)
	    	  	_obj = data;
	    	  	$(".friHang").empty();
	    	  	_obj.Id = data.Id;
	    	  	_obj.Code = data.Code;
	    	  	_obj.Descrition=data.Descrition;
	    	  	className.value = data.Name;
	    	  	sort.value = data.OrderId;
	    	  	_obj.CreateTime = data.CreateTime;
	    	  	pcode = data.Pcode;
	    		if(data.Pcode=="00"){
	    			$(".classify").find("option[value='"+0+"']").prop("selected",true);
	    			
	    		}else{
	    			$(".classify").find("option[value='"+data.Pcode+"']").prop("selected",true);
	    			
	    		}
	    		
	    		
	    		
	    		  renderForm();
	    	  }
	    	  
	      });
      }else{
      	
      	
      	
      }
      
      
//    confirm.addEventListener("click",_confirm)
			form.on('submit(queding)',_confirm)
      
	      function _confirm(){
			_obj.Name = className.value;
	      	_obj.OrderId = sort.value;
	      	_obj.Pcode = pcode;
//	      	console.log("pcode",)
	      	
	      	if(params == null){
	      		_obj.CreateTime = getNowFormatDate();
	      		console.log("_obj",_obj)
	      		DBHelper.Post("Admin_Xn_Trade", _obj, function(data) {	
							console.log(data)
							let index = layer.alert("操作成功",function(){
										window.location.href="#/layui/hangyeList";
										layer.close(index)
									})
//							window.location.href="#/layui/hangyeList";
						}, function(data) {
							console.log(data)
						});
	      		
	      	}else {
	      		_obj.UpdateTime = getNowFormatDate();
	      		console.log("_obj",_obj)
	      		console.log("pcode",pcode)
	      		DBHelper.Put("Admin_Xn_Trade?id="+params.id+"&Pcode="+pcode, _obj, function(data) {
							console.log(data)
							let index = layer.alert("操作成功",function(){
										window.location.href="#/layui/hangyeList";
										layer.close(index)
									})
//							window.location.href="#/layui/hangyeList";
						}, function(data) {
							console.log(data)
						});
	      	}
	      	
	      	
		}
    })
	
	
	
	
	
	
	
	 function renderForm(){
		  layui.use('form', function(){
		   var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
		   form.render();
		  });
		}
</script>


	<style scoped>
				/*.fhImg-warp{
					width: 60px;
					min-height: 60px;
					display: block;
					position: relative;
				}
				.fhImg{
					width: 100%;
					height: 100%;
					left: 0;
					top: 0;
					position: absolute;
				}*/
				.small_td{
					width: 100px;
					text-align: right;
				}
      	.title{
      		margin-left: 0px;
      		margin-top: 30px;
      	}
      	.btn{
      		margin: 0 auto;
      		text-align: center;
      		
      	}
      	.btn button,.btn a{
      		width: 200px;
      	}
      	.checkBtn{
      		border-radius: 5px;
      		width: 100px;
      	}
      	.confirm{
      		margin-bottom: 50px;
      	}
      	.listBtns{
      		display: flex;
      		justify-content: space-between;
      		width: 100%;
      	}
      	.listBtn{
      		border-radius: 5px;
      	}
      	.haveTo{
      		color: red;
      	}
      	.addFHBtn{
      		text-align: center;
      		margin-top: 20px;
      		margin-bottom: 20px;
      	}
      	.addFHBtn button{
      		display: inline-block;
      		width: 300px;
      	}
      </style>
      
      
